package com.listener;
import java.io.Serializable;
import java.text.MessageFormat;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 实现了ServletContextListener接口,因此可以对ServletContext对象的创建和销毁这两个动作进行监听。
 * 实现了ServletContextAttributeListener接口,因此可以对ServletContext对象中属性的变更的事件监听器
 * ServletContext域对象创建和销毁时机：
 *　　　 创建：服务器启动针对每一个Web应用创建ServletContext
 *     销毁：服务器关闭前先关闭代表每一个web应用的ServletContext
 *     
 * 实现了HttpSessionListener接口,因此可以对HttpSession对象的创建和销毁这两个动作进行监听。
 * 实现了HttpSessionAttributeListener接口,因此可以对Session对象中属性的变更的事件监听器
 * 实现了HttpSessionBindingListener接口,因此JavaBean对象可以感知自己被绑定到 Session中和从Session中删除的事件
 * 实现了HttpSessionActivationListener接口,因此JavaBean对象可以感知自己被活化(反序列化)和钝化(序列化)的事件
 *
 * 实现了ServletRequestListener接口,因此可以对ServletRequest对象的创建和销毁这两个动作进行监听。
 * 实现了ServletRequestAttributeListener接口,因此可以对ServletRequest对象中属性的变更的事件监听器
 *    创建：用户每一次访问都会创建request对象
 *　　  销毁：当前访问结束，request对象就会销毁
 *
 *实现了Serializable接口,因此可序列化
 */
public class TestServletContextListener implements ServletContextListener,HttpSessionListener,ServletRequestListener, 
                          ServletContextAttributeListener,HttpSessionAttributeListener,ServletRequestAttributeListener,                         
	                      HttpSessionBindingListener,HttpSessionActivationListener,Serializable{
 /** +++++++++++++++++++++++++++ServletContextListener接口 ++++++++++++++++++++++++++**/	
			
	
	/**
	 * ServletContext对象创建时监听 
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		 System.out.println("--------------------ServletContext对象创建(监听)--------------------");
		
	}	
	//ServletContext对象销毁时监听
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		  System.out.println("-------------------ServletContext对象销毁(监听)--------------------");	
	}
	
	//================================ServletContextAttributeListener接口========================================
		
	/**
	 * ServletContext对象添加时监听
	 */
	@Override
	public void attributeAdded(ServletContextAttributeEvent scab) {
		String str =MessageFormat.format(
                "ServletContext域对象中添加了属性:{0}，属性值是:{1}"
                ,scab.getName()
                ,scab.getValue());
        System.out.println(str);		
	}
	
	/**
	 * ServletContext对象删除时监听
	 */
	@Override
	public void attributeRemoved(ServletContextAttributeEvent scab) {
		String str =MessageFormat.format(
                "ServletContext域对象中删除属性:{0}，属性值是:{1}"
                ,scab.getName()
                ,scab.getValue());
        System.out.println(str);
		
	}

	/**
	 * ServletContext对象替换时监听
	 */
	@Override
	public void attributeReplaced(ServletContextAttributeEvent scab) {
		String str =MessageFormat.format(
                "ServletContext域对象中替换了属性:{0}的值"
                ,scab.getName());
        System.out.println(str);
		
	}
	
/** +++++++++++++++++++++++++++++HttpSessionListener接口 ++++++++++++++++++++++++++**/		
	
	/**
	 * session对象创建时监听
	 */
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		  System.out.println("session对象创建================"+ se.getSession() + "创建了！！");
		
	}
	
	/**
	 * session对象销毁时监听
	 */
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		 System.out.println("session对象销毁了！！=============");			 
	}
  //================================HttpSessionAttributeListener接口========================================	
	
	/**
	 * session对象添加时监听
	 */
	@Override
	public void attributeAdded(HttpSessionBindingEvent se) {
		String str =MessageFormat.format(
                "HttpSession域对象中添加了属性:{0}，属性值是:{1}"
                ,se.getName()
                ,se.getValue());
        System.out.println(str);
		
	}
	
	/**
	 * session对象删除时监听
	 */
	@Override
	public void attributeRemoved(HttpSessionBindingEvent se) {
	     String str =MessageFormat.format(
	                "HttpSession域对象中删除属性:{0}，属性值是:{1}"
	                ,se.getName()
	                ,se.getValue());
	        System.out.println(str);
		
	}
	
	/**
	 * session对象替换时监听
	 */
	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {
        String str =MessageFormat.format(
                "HttpSession域对象中替换了属性:{0}的值"
                ,se.getName());
        System.out.println(str);
		
	}

 //================================HttpSessionBindingListener接口========================================	
	
	 private String name;
	 private static final long serialVersionUID = 7589841135210272124L;
	 public String getName() {
	        return name;
	 }
     public void setName(String name) {
         this.name = name;
     }
     public TestServletContextListener() {
          this.name = name;
     }
     public TestServletContextListener(String name) {
         this.name = name;
     }
	
     /**
      * JavaBean被绑定到session对象时监听
      */
	@Override
	public void valueBound(HttpSessionBindingEvent arg0) {
		 System.out.println(name+"被加到session中了==============");		
	}
	
	/**
	 * JavaBean从session对象删除时监听
	 */
	@Override
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println(name+"被session踢出来了===============");		
	}
	
//================================HttpSessionActivationListener接口========================================		
		
	/**
	 * JavaBean随session对象钝化时监听
	 */
	@Override
	public void sessionWillPassivate(HttpSessionEvent se) {
		System.out.println(name+"和session一起被序列化(钝化)到硬盘了，session的id是："+se.getSession().getId());
		
	}
	
	/**
	 * JavaBean随session对象活化时监听
	 */
	@Override
	public void sessionDidActivate(HttpSessionEvent se) {
		 System.out.println(name+"和session一起从硬盘反序列化(活化)回到内存了，session的id是："+se.getSession().getId());
		
	}
	
			
/** +++++++++++++++++++++++++++++ServletRequestListener接口 ++++++++++++++++++++++++++**/	
	

	/**
	 * Request对象创建时监听	
	 */
	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		// System.out.println("Request对象=============="+sre.getServletRequest() + "创建了！！");		
	}
	
	/**
	 * Request对象创建时监听
	 */
	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		//System.out.println("Request对象==============="+sre.getServletRequest() + "销毁了！！");
		
	}
  //================================ServletRequestAttributeListener接口========================================	
	
	/**
	 * Request对象添加时监听
	 */
	@Override
	public void attributeAdded(ServletRequestAttributeEvent srae) {
	      String str =MessageFormat.format(
	                "ServletRequest域对象中添加了属性:{0}，属性值是:{1}"
	                ,srae.getName()
	                ,srae.getValue());
	        System.out.println(str);		
	}
	
	/**
	 * Request对象删除时监听
	 */
	@Override
	public void attributeRemoved(ServletRequestAttributeEvent srae) {
	       String str =MessageFormat.format(
	                "ServletRequest域对象中删除属性:{0}，属性值是:{1}"
	                ,srae.getName()
	                ,srae.getValue());
	        System.out.println(str);		
	}
	
	/**
	 * Request对象替换时监听
	 */
	@Override
	public void attributeReplaced(ServletRequestAttributeEvent srae) {
        String str =MessageFormat.format(
                "ServletRequest域对象中替换了属性:{0}的值"
                ,srae.getName());
        System.out.println(str);		
	}



	
		
}
